package com.mzj.saas.mservice.property.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.mzj.saas.commons.ApiData;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.property.service.PropertyCommunityHouseService;
import com.mzj.saas.mservice.property.vo.ChangerOwnerVo;
import com.mzj.saas.mservice.property.vo.PropertyCommunityHouseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 租客租户管理
 *
 * @author hikari
 * @since 2021-05-26
 */
@RestController
@RequestMapping("/property/communityHouse")
@Api(tags = "租客租户管理")
public class PropertyCommunityHouseController {
    @Autowired
    private PropertyCommunityHouseService propertyCommunityHouseService;

    /**
     * 租客租户列表
     * @param determineType    判断类型： 1：则要分页   null或者是2：不要分页，此时由于点击了导出按钮
     * @param accessToken
     * @param searchCondition
     * @param houseId
     * @param communityId
     * @param buildId
     * @param unitId
     * @param roomNumber
     * @param floor
     * @param ownerName
     * @param ownerMobile
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping
    @ApiOperation("房屋列表")
    public ResultBean<ApiData<PropertyCommunityHouseVO>> listBy(@RequestHeader String accessToken,
                                                                @RequestParam(required = false) Integer determineType,
                                                                @RequestParam(required = false) String searchCondition,
                                                                @RequestParam(required = false) String houseId,
                                                                @RequestParam(required = false) String communityId,
                                                                @RequestParam(required = false) Long buildId,
                                                                @RequestParam(required = false) Long unitId,
                                                                @RequestParam(required = false) String roomNumber,
                                                                @RequestParam(required = false) Integer floor,
                                                                @RequestParam(required = false) String ownerName,
                                                                @RequestParam(required = false) String ownerMobile,
                                                                @RequestParam(required = false) Integer userCount,
                                                                @RequestParam(required = false) Long orgId,
                                                                @RequestParam(defaultValue = "0") Integer pageNumber,
                                                                @RequestParam(defaultValue = "5") Integer pageSize) {
        ResultBean<ApiData<PropertyCommunityHouseVO>> apiDataResultBean = propertyCommunityHouseService.listBy(accessToken, determineType, searchCondition, houseId, communityId, buildId, unitId,
                roomNumber, floor, ownerName, ownerMobile, userCount, null, pageNumber, pageSize);
        return apiDataResultBean;
    }

    /**
     * 租客租户列表
     * @param determineType    判断类型： 1：则要分页   null或者是2：不要分页，此时由于点击了导出按钮
     * @param accessToken
     * @param searchCondition
     * @param houseId
     * @param communityId
     * @param buildId
     * @param unitId
     * @param roomNumber
     * @param floor
     * @param ownerName
     * @param ownerMobile
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/filter/apartment")
    @ApiOperation("过滤房屋列表")
    public ResultBean<ApiData<PropertyCommunityHouseVO>> listByFilterApartment(@RequestHeader String accessToken,
                                                                               @RequestParam(required = false) Integer determineType,
                                                                               @RequestParam(required = false) String searchCondition,
                                                                               @RequestParam(required = false) String houseId,
                                                                               @RequestParam(required = false) String communityId,
                                                                               @RequestParam(required = false) Long buildId,
                                                                               @RequestParam(required = false) Long unitId,
                                                                               @RequestParam(required = false) String roomNumber,
                                                                               @RequestParam(required = false) Integer floor,
                                                                               @RequestParam(required = false) String ownerName,
                                                                               @RequestParam(required = false) String ownerMobile,
                                                                               @RequestParam(required = false) Integer userCount,
                                                                               @RequestParam(required = false) Long orgId,
                                                                               @RequestParam(defaultValue = "0") Integer pageNumber,
                                                                               @RequestParam(defaultValue = "5") Integer pageSize) {
        ResultBean<ApiData<PropertyCommunityHouseVO>> apiDataResultBean = propertyCommunityHouseService.listBy(accessToken, determineType, searchCondition, houseId, communityId, buildId, unitId,
                roomNumber, floor, ownerName, ownerMobile, userCount, orgId, pageNumber, pageSize);
        if (CollectionUtil.isNotEmpty(apiDataResultBean.getResultData().getData())) {
            List<PropertyCommunityHouseVO> list = apiDataResultBean.getResultData().getData().stream().filter(x -> x.getHouseNature() != 7).collect(Collectors.toList());
            apiDataResultBean.getResultData().setData(list);
            apiDataResultBean.getResultData().setTotal((long) list.size());
        }
        return apiDataResultBean;
    }

    /**
     * 更改产权所有人
     *
     * @param accessToken
     * @return
     */
    @PostMapping("/owner")
    @ApiOperation("更改产权所有人")
    public ResultBean<Object> changerOwner(@RequestHeader String accessToken,
                                           @RequestBody ChangerOwnerVo changerOwnerVo) {
        String ownerMobile = changerOwnerVo.getOwnerMobile();
        String houseId = changerOwnerVo.getHouseId();
        String ownerName = changerOwnerVo.getOwnerName();
        Integer orgId = changerOwnerVo.getOrgId();
        Integer ownerType = changerOwnerVo.getOwnerType();

        return propertyCommunityHouseService.changeOwner(accessToken, houseId, ownerName, ownerMobile,orgId,ownerType);
    }
}
